#
# Meta coding by Macro.
#
# Input arguments:
# 	$(META_SRCDIRS)		- The full path of meta source files (.meta.S)
# 	$(META_INCDIRS)		- The full path of header files (.h)
#	$(META_CC)			- C compiler, It requires preprocessing parameters (e.g. -E -P)
# Optional input arguments:
#	$(META_SUFFIX)		- The suffix of meta files (default: meta.S)
#	$(META_PROJPATH)	- The full path of project, Only affect the output log
#

# -------- parameters checkout

ifeq ($(META_SRCDIRS),)
$(error META_SRCDIRS should not be null)
endif
ifeq ($(META_INCDIRS),)
$(error META_INCDIRS should not be null)
endif
ifeq ($(META_CC),)
$(error META_CC should not be null)
endif

# -------- verbose contorl, `make V=1` to show the detail of compile

SILENT			 = @
MSG				 = @echo META: $(subst $(META_PROJPATH)/,,$<)
ifeq ($(V),1)
SILENT			 =
MSG				 =
endif

# -------- makefile's arguments

META_SUFFIX		?= meta.S

# -------- generate target file full path

# get the source files in `META_SRCDIRS`
METAFILES		:= $(foreach dir,$(META_SRCDIRS),$(wildcard $(dir)/*.$(META_SUFFIX)))

# generate meta objects
METAOBJS		:= $(patsubst %.$(META_SUFFIX),%,$(METAFILES))

# -------- include path

I				:= $(foreach dir,$(META_INCDIRS),-I$(dir))

# -------- rules

# set default target
.DEFAULT_GOAL	:= ALL

# target files (a collection)
ALL : $(METAOBJS)

% : %.$(META_SUFFIX)
	$(SILENT) mkdir -p $(@D)
	$(SILENT) $(META_CC) $(I) -o $@ $<
	$(MSG)

# -------- pseudo target

.PHONY: clean

clean:
	$(SILENT) rm -rf $(METAOBJS)
